package com.example.cart_system.jwt;

import io.jsonwebtoken.*;
import io.jsonwebtoken.security.Keys;

import javax.crypto.SecretKey;
import java.util.*;
import java.util.concurrent.TimeUnit;

public class JwtUtil {

    private static final Long JWT_EXPIRATION = 60 * 60 * 12 * 7L;
    private static final SecretKey key = Keys.secretKeyFor(SignatureAlgorithm.HS256);

    public static String generateToken(String usrName, String usrId) {
        Map<String, Object> claims = new HashMap<>();
        claims.put("userName", usrName);
        claims.put("usrId", usrId);
        return Jwts.builder()
                .setClaims(claims)
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + JWT_EXPIRATION))
                .signWith(SignatureAlgorithm.HS256, key)
                .compact();
    }

    public static Claims parseToken(String token) throws JwtException {
        return Jwts.parserBuilder()
                .setSigningKey(key)
                .build()
                .parseClaimsJws(token)
                .getBody();
    }
}
